package springmaven;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.apache.logging.log4j.web.Log4jServletContextListener;
import org.directwebremoting.servlet.DwrServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class DLRYWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class<?>[] { RootConfig.class, SecurityConfig.class };
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class<?>[] { WebConfig.class };
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		// 添加自定义Listener关闭JDBC Connection
		servletContext.addListener(CloseAbandonedConnectionCleanupThreadListener.class);
		// 不添加Log4jServletContextListenser监听关闭tomcat时回报内存泄露
		servletContext.addListener(Log4jServletContextListener.class);
		super.onStartup(servletContext);
		Dynamic myServlet = servletContext.addServlet("myServlet", DwrServlet.class);
		myServlet.addMapping("/dwr/*");
		Map<String, String> mapInit = new HashMap<String, String>();
		mapInit.put("debug", "true");
		mapInit.put("crossDomainSessionSecurity", "false");
		mapInit.put("allowScriptTagRemoting", "true");
		mapInit.put("initApplicationScopeCreatorsAtStartup", "true");
		mapInit.put("activeReverseAjaxEnabled", "true");
		mapInit.put("pollAndCometEnabled", "true");
		myServlet.setInitParameters(mapInit);
		myServlet.setLoadOnStartup(1);
		System.out.println("addMyServlet");
	}

}
